/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.aun.news;

import java.util.List;

import br.org.groupwareworkbench.core.framework.CollabletInstance;
import br.org.groupwareworkbench.core.bd.DAOFactory;

public class NewsMgrInstance extends CollabletInstance {

    private final NewsDAO newsDAO = DAOFactory.get(NewsDAO.class);
    private int numRelatedNews = 3; // TODO: Não é thread-safe.

    public NewsMgrInstance() {
        super();
    }

    @Override
    public void init() {
        
        this.newsDAO.copyFromAUN(this.getId());
    }

    @Override
    public void destroy() {
    	this.newsDAO.deleteByIdInstance(this.getId());
    }

    public void copyFromAUN(long id) {
        this.newsDAO.copyFromAUN(id);
    }
    
    public List<News> list() {
        return this.newsDAO.listByIdInstance(this.getId());
    }

    public News findById(long id) {
        return this.newsDAO.findById(id);
    }

    public void save(News news) {
        boolean isNew = (news.getId() == 0);
        news.setIdInstance(this.getId());
        this.newsDAO.save(news, isNew);
        
    }

    public void delete(Long id) {
        this.newsDAO.deleteById(id);
    }
    
    public int getNumRelatedNews() {
        return numRelatedNews;
    }

    public void setNumRelatedNews(int numRelatedNews) {
        this.numRelatedNews = numRelatedNews;
    }
}
